﻿Public Class frmPhongHoc

    Sub LoadDanhSach()
        Dim _ph As DataTable
        _ph = PhongHocDAO.SelectAll()
        dgvTTPhongHoc.DataSource = _ph
    End Sub

    Sub Display()
        If (dgvTTPhongHoc.SelectedRows.Count > 0) Then
            txtMAPHONG.Text = dgvTTPhongHoc.SelectedRows(0).Cells(0).Value.ToString()
            txtTENPHONG.Text = dgvTTPhongHoc.SelectedRows(0).Cells(1).Value.ToString()
            txtDIADIEM.Text = dgvTTPhongHoc.SelectedRows(0).Cells(2).Value.ToString()
        End If
    End Sub

    Private Sub frmPhongHoc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvTTPhongHoc.Rows.Clear()
        LoadDanhSach()
    End Sub

    Private Sub dgvTTPhongHoc_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvTTPhongHoc.CellContentClick
        Display()
    End Sub

    Private Sub dgvTTPhongHoc_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvTTPhongHoc.SelectionChanged
        Display()
    End Sub

    Private Sub frmPhongHoc_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If (dgvTTPhongHoc.SelectedRows.Count > 0) Then
            If MessageBox.Show("Bạn có muốn lưu lại những thay đổi", "Thông Báo", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                Dim _ph As PhongHocDTO = New PhongHocDTO()
                _ph.MaPhong = txtMAPHONG.Text
                _ph.TenPhong = txtTENPHONG.Text
                _ph.DiaDiem = txtDIADIEM.Text
                Try
                    PhongHocDAO.Update(_ph)
                    MessageBox.Show("Cập nhật phòng học thành công.")
                Catch ex As Exception
                    MessageBox.Show(ex.ToString())
                End Try
                LoadDanhSach()
            End If
        End If
    End Sub

    Private Sub btnTHEM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTHEM.Click
        Dim _ph As PhongHocDTO = New PhongHocDTO()
        _ph.TenPhong = txtTENPHONG.Text
        _ph.DiaDiem = txtDIADIEM.Text
        Try
            PhongHocDAO.Insert(_ph)
            MessageBox.Show("Thêm phòng học thành công.")
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
        LoadDanhSach()
    End Sub

    Private Sub btnSUA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSUA.Click
        If (dgvTTPhongHoc.SelectedRows.Count > 0) Then
            Dim _ph As PhongHocDTO = New PhongHocDTO()
            _ph.MaPhong = txtMAPHONG.Text
            _ph.TenPhong = txtTENPHONG.Text
            _ph.DiaDiem = txtDIADIEM.Text
            Try
                PhongHocDAO.Update(_ph)
                MessageBox.Show("Cập nhật phòng học thành công.")
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            End Try
            LoadDanhSach()
        End If
    End Sub

    Private Sub btnXOA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXOA.Click
        Dim _ph As PhongHocDTO = New PhongHocDTO()
        _ph.MaPhong = txtMAPHONG.Text
        Try
            PhongHocDAO.Delete(_ph)
            MessageBox.Show("Xóa dữ liệu thành công.")
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
        LoadDanhSach()
    End Sub

    Private Sub btnTHOAT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTHOAT.Click
        Me.Close()
    End Sub
End Class